<template>
  <el-dialog title="分配权限" :visible="isShow" @close="closeEvent">
    <el-tree
      ref="tree"
      :data="treeData"
      :props="{ label: 'name' }"
      show-checkbox
      node-key="id"
      check-strictly
    />
    <template #footer>
      <div style="text-align: center">
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermission } from '@/api/permission.js'
import { changeTreeData } from '@/utils/index'
import { sysRoleGet, sysRoleAssignPrem } from '@/api/setting'
export default {
  name: 'DiaLogPower',
  model: {
    prop: 'isShow'
  },
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      treeData: [],
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    //  关闭弹框
    closeEvent() {
      this.$emit('input', false)
      this.$refs.tree.setCheckedKeys([])
    },
    //  获取所有权限点
    async getData() {
      const res = await sysPermission()
      this.treeData = changeTreeData(res.data, '0')
      console.log('所有-权限点', res)
    },
    // 获取角色详情
    async getUserInfo(id) {
      this.id = id
      const res = await sysRoleGet(id)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
      console.log('获得角色详情', res)
    },
    //  点击确定
    async submit() {
      //   console.log('hhh', this.$refs.tree.getCheckedKeys())
      await sysRoleAssignPrem({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('分配权限成功')
      this.closeEvent()
    }
  }
}
</script>

<style></style>
